﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.标签打印.Service;
using DXApplicationTangche.UC.门店出入库.Service;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using Seagull.BarTender.Print;
using DXApplicationTangche.UC.生产计划.生产批生产.service;
using DXApplicationTangche.UC.下单MTO.Service;
using DXApplicationTangche.service;
using System.IO;
using System.Text.RegularExpressions;
using DXApplicationTangche.DTO;
using System.Drawing.Printing;
using DXApplicationTangche.UC.金蝶;
using DXApplicationTangche.Utils;
using mendian;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DevExpress.Utils.Win;
using DXApplicationTangche.UC.面料库存.入库;

namespace DXApplicationTangche.UC.标签打印
{
    public partial class Form外购水洗标 : DevExpress.XtraEditors.XtraForm
    {
        private List<washDTO> washDTOs { get; set; } = new List<washDTO>();
        List<外购DTO> dTOTags { get; set; } = new List<外购DTO>();
        private List<String> barcodes = new List<string>();
        private String standard;
        private String styleSizeCd;
        
        public Form外购水洗标()
        {
            InitializeComponent();
            List<FabricContentDTO> fabricContentDTOs = new List<FabricContentDTO>();
            #region
            fabricContentDTOs.Add(new FabricContentDTO("XS"));
            fabricContentDTOs.Add(new FabricContentDTO("S"));
            fabricContentDTOs.Add(new FabricContentDTO("M"));
            fabricContentDTOs.Add(new FabricContentDTO("L"));
            fabricContentDTOs.Add(new FabricContentDTO("XL"));
            fabricContentDTOs.Add(new FabricContentDTO("XXL"));
            fabricContentDTOs.Add(new FabricContentDTO("XXXL"));
            #endregion
            this.searchLookUpEdit含量.Properties.DataSource = fabricContentDTOs;
        }
        public Form外购水洗标(String barcode)
        {
            InitializeComponent();
            washDTO washDTO = 标签打印Service.getWarterMarkPrintLogByBarcode(barcode);
            try { washDTO.styleSizeCd = washDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { };
            this.washDTOs.Add(washDTO);
            this.gridControl水洗标.DataSource = this.washDTOs;
        }
        private void 刷新()
        {
        }
        #region
        private void searchLookUpEdit含量_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear含量Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit含量View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit含量.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.styleSizeCd = this.searchLookUpEdit含量.Properties.View.GetRowCellValue(rowHandle, "content").ToString();
                }
                //this.textBox修改含量.Text = this.contentPercent;
                //this.textBox供应商名称.Text = this.fname;
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear含量Btn_Click(object sender, EventArgs e)
        {
            //this.searchLookUpEdit供应商.ToolTip = null;
            //searchLookUpEdit供应商.EditValue = null;
        }
        private void searchLookUpEdit含量_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
            {
                e.DisplayText = this.styleSizeCd;
            }
        }
        #endregion
        private void repositoryItemButtonEdit1_Click(object sender, EventArgs e)
        {
            try
            {
                //  根据款式查询工信部质量标准
                String styleNameCn = this.washDTOs[this.gridView水洗标.FocusedRowHandle].styleNameCn;
                this.standard = 标签打印Service.getMIITStandardByStyleCode(styleNameCn);
            }
            catch
            {
                this.standard = "FZ/T 73020-2019";
            }

            try
            {
                String printFileName = @"tag\" + "PrintConfig.xml";
                if (!FileUtil.FileExist(printFileName))
                {
                    //将传参的打印机名称保存到配置文件中
                    PrintMachineDTO printMachineDTO传参 = new PrintMachineDTO();
                    new Form打印机配置文件(printMachineDTO传参).ShowDialog();
                    DealXML.ObjectToXMLFile(new PrintMachineDTO("01", printMachineDTO传参.printName), printFileName, Encoding.UTF8);
                }
                PrintMachineDTO printMachineDTO = DealXML.XMLFlieToObject<PrintMachineDTO>(FileUtil.getAbsolutePath(printFileName), Encoding.UTF8);

                String result = 标签打印Service.print水洗(this.dTOTags[this.gridView水洗标.FocusedRowHandle], false, printMachineDTO.printName);
                //if (!"Success".Equals(result)) {
                //    MessageBox.Show("打印异常，请联系李金键解决");
                //}
;
                //标签打印Service.print套号水洗(this.washDTOs[this.gridView水洗标.FocusedRowHandle], false);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

         }

         

        private void simpleButton上传打印模板_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "btw|*.btw";
            ofd.Title = "请选择btw文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                String str = 标签打印Service.uploadPrintFile(ofd);
                if (!String.IsNullOrWhiteSpace(str))
                {
                    MessageBox.Show("上传成功");
                }
                else
                {
                    MessageBox.Show("上传失败");
                }
            }
        }


        private void simpleButton确认_Click(object sender, EventArgs e)
        {
            this.dTOTags = 标签打印Service.getGarmentTagPrintList(this.textBox款式.Text.ToString(), "IGS_GROUP_SIZE-"+this.styleSizeCd, ValueUtil.StringToInt(this.textBox数量.Text.ToString()));
            this.gridControl水洗标.DataSource = this.dTOTags;
        }



        private void textBox水洗_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (!String.IsNullOrWhiteSpace(this.textBox水洗.Text))
                {
                    try
                    {
                        订单一览新.订单预览新.service.PlainMessageResult re = 标签打印Service.purchaseGarmentStockInOut(this.textBox水洗.Text,1,"12");
                        if (!"200".Equals(re.errorCode))
                        {
                            MessageBox.Show(re.errorMessage);
                        }
                        this.textBox水洗.Clear();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    MessageBox.Show("请扫码");
                }
            }
        }

        private void textBox出库_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (!String.IsNullOrWhiteSpace(this.textBox出库.Text))
                {
                    try
                    {
                        订单一览新.订单预览新.service.PlainMessageResult re = 标签打印Service.purchaseGarmentStockInOut(this.textBox出库.Text, 0, "12");
                        if (!"200".Equals(re.errorCode))
                        {
                            MessageBox.Show(re.errorMessage);
                        }
                        this.barcodes.Add(this.textBox出库.Text);
                        this.刷新();
                        this.textBox出库.Clear();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    MessageBox.Show("请扫码");
                }
            }
        }
    }
}
